package at.main.gui;

import java.util.Random;

import at.main.android.Graphics;
import at.main.android.Image;
import at.main.android.Sprite;
import at.main.util.ConfigPanel;
import at.main.util.ConstGame;

public class Monster {
	public int iStatus = 0;

	public static final int Left = 1;

	public static final int Right = 0;

	public static final int Up = 3;

	public static final int Down = 2;

	public static final int k_ran = 100;

	public int iX = 0;

	public int iY = 0;

	public int iV = 0;

	public boolean isLife = true; // con sống hay chết

	public int iIndex = 0;

	// public int iStartCol = 0;
	//
	// public int iEndCol = 0;
	//
	// public int iStartRow = 0;
	//
	// public int iEndRow = 0;
	public final int k_CountTile = 4;

	public int iNumColofCanvas = 0;

	public int iNumRowofCanvas = 0;

	public int iWidthMap = 0;

	public int iHeightMap = 0;

	public int iCurrCol = 0;

	public int iCurrRow = 0;

	public boolean isCheckGo = true;

	public Image[] img_arrMonster = null;

	public int iK = 0; // mot lan chon thi dich chuyen 4 buoc

	public Random ran;

	public Monster() {
		
	}

	public Monster(int x, int y, int v, Image[] img) {

		ran = new Random();
		ran.setSeed(System.currentTimeMillis());
		this.iIndex = 0;
		this.iX = x;
		this.iY = y;
		this.iV = v;
		this.img_arrMonster = new Image[k_CountTile];

		for (int i = 0; i < 4; i++) {
			this.img_arrMonster[i] = img[i];
		}

		this.iNumColofCanvas = iX / ManagerMonster.iSizeMonster;
		this.iNumRowofCanvas = (iY - ConfigPanel.heightPanel())
				/ ManagerMonster.iSizeMonster;

		this.iStatus = Up;
		this.isLife = true;
		this.iK = 0;
//		System.out.println("<<<<<<<< iX " + iX + " iY :" + iY + "iStatus "
//				+ iStatus);
	}

	public void paint(Graphics g) {
		if (ManagerMonster.isState) {
			g.drawRegion(this.img_arrMonster[this.iStatus], 0,
					this.iIndex * 24, 24, 24, Sprite.TRANS_NONE, this.iX,
					this.iY, Graphics.LEFT | Graphics.TOP);
		} else {
			g.drawRegion(this.img_arrMonster[this.iStatus], 0,
					(ConstGame.iCountTileMonster - 1) * 24, 24, 24,
					Sprite.TRANS_NONE, this.iX, this.iY, Graphics.LEFT
							| Graphics.TOP);
		}
	}

	public boolean CheckMonsterGo(int Row, int Col) {
		Col = Col - ConfigPanel.heightPanel();
		if (Col < 0 || Row < 0)
			return false;
		Row = Row / ManagerMonster.iSizeMonster;
		Col = Col / ManagerMonster.iSizeMonster;
		if (Col > 28 || Row > 28)
			return false;
		switch (MyCanvas.instanMyCanvas.playGame.arrMap[Col][Row]) {
		case 13:
			return false;
		}
		return true;
	}

	// Chon huong cho con monster
	public final int k_anpa = ManagerMonster.iSizeMonster - iV;

	public int ChooseDirection() {
		int iTemp = 0;
		boolean flag = true;
		int nRandom = 4;
//		ran = new Random(); không nên đặt trong hàm này mà nên đặt ở hàn khỏi tạo
//		ran.setSeed(System.currentTimeMillis());
		
		do {
			iTemp = Math.abs(ran.nextInt() % nRandom);			
			// Right
			if (iTemp == 0) {
				if (CheckMonsterGo(this.iX + ManagerMonster.iSizeMonster, iY)) {
					return 0;
				}
			}
			// Left
			if (iTemp == 1) {
				if (CheckMonsterGo(iX - ManagerMonster.iSizeMonster,iY)) {
					return 1;
				}
			}
			// Down
			if (iTemp == 2) {
				if (CheckMonsterGo(iX, iY+ ManagerMonster.iSizeMonster)) {
					return 2;
				}
			}
			// Up
			if (iTemp == 3) {
				if (CheckMonsterGo(iX, iY	- ManagerMonster.iSizeMonster)) {
					return 3;
				}
			}			
		} while (flag);
		System.out.println(">>>>>>ket thuc ");
		return 0;
	}
//	Kiem tra va cham cua pacman voi monster
	// x1 , y1 : Toa do con monster1 
	// x2 , y 2 : Toa do con monster2
	public boolean collision(int x1, int y1, int x2, int y2) 
	{
		if (x2 <= x1+24 && x1 <= x2+24 && y2 <= y1 && y1 <= y2+ 24)
			return true;
		return false;
	}
	
	
	public void update() {
		int iTemp = ManagerMonster.iSizeMonster / iV; // 24 / 4 = 6
		if (PlayGame.isReady) {
			// Right
			int Status =0;
			if (iK == 0) {
				Status = ChooseDirection();				
				iStatus = Status;
			}
			iK++;
			if (iK > (iTemp-1)) {//if (iK > 5) {
				iK = 0;
			}
			Status = iStatus;
			switch (Status) {
			case 0: // Right
				iX += iV;
				break;
			case 1:// Left
				iX -= iV;
				break;
			case 2: // Down
				iY += iV;
				break;
			case 3: // Up
				iY -= iV;
				break;
			default:
				break;
			}
			if (ManagerMonster.isState) {
				this.iIndex++;
				if (this.iIndex > ConstGame.iCountTileMonster - 2) {
					this.iIndex = 0;
				}
			}		
		}
	}
}
